/**
 * 首页
 */
 /* 重置layout的大小 在加载完成数据之后执行，保证动态设置高度*/
function setHeight(layoutId) {
	var defaultId = "cmLayout";
	if(layoutId){
		defaultId = layoutId;
	}
	var c = $('#'+defaultId);
	var p = c.layout('panel', 'center'); // get the center panel
	var oldHeight = p.panel('panel').outerHeight();
	p.panel('resize', {height : 'auto'});
	var newHeight = p.panel('panel').outerHeight();
	c.height(c.height() + newHeight - oldHeight);
	c.layout('resize');
	
}

function getCenterHeight(layoutId){
	var defaultId = "cmLayout";
	if(layoutId){
		defaultId = layoutId;
	}
	var c = $('#'+defaultId);
	var p = c.layout('panel', 'center'); // get the center panel
	//var height = p.panel('panel').outerHeight();
	//alert(p.outerHeight()+" : "+p.innerHeight()+" : "+p.height());
	return p.innerHeight();
}

function addTab(tabObj,tabId){
	var defaultTabId = "cmTabs";
	if(tabId){
		defaultTabId = tabId;
	}
	var tabDefault = {
		href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/tabDemo.jsp",
		title:"新Tab",
		selected: true,
		closable:true
	};
	var options ;
	if(tabObj){
	    var empty = {};
		options = $.extend(empty,tabDefault,tabObj);
	}else{
		options = tabDefault;
	}
	if($('#'+defaultTabId).tabs("exists",options.title)){
		$('#'+defaultTabId).tabs('select',options.title);
	}else{
	    $('#'+defaultTabId).tabs('add',options);
	}
//	alert(tab.outerHeight()+":"+tab.innerHeight());
//    alert("index:"+index);
	//var oldHeight = p.panel('panel').outerHeight();  
	//setHeight();
}

function getPageParams(){
	var href = window.location.href;
	if(!href){
		return {};
	}
	var quesIndex = href.indexOf("?");
	if(-1 == quesIndex){
		return {};
	}
	var paramStr = href.substring(quesIndex+1);
	//alert("paramStr:"+paramStr);
	var para = paramStr.split("&");
	var item;
	var items;
	var result ="";
	var appendFlag = false;
	for(var i = 0 ;i < para.length;i++){
		item = para[i];
		if(!item){
			continue;
		}
		items = item.split("=");
		if(items.length != 2){
			continue;
		}
		if("_" == items[0]){
			continue;
		}
		if(appendFlag){
		   result = result+",\""+items[0] +"\":\""+ items[1]+"\"";
		}else{
		   result = "\""+items[0] +"\":\""+ items[1]+"\"";
		   appendFlag = true;
		}
	}
	//alert('{'+result+'}');
	return jQuery.parseJSON('{'+result+'}');
}

/**
 * 
 * @param {} params
 * @param String appendPage "false"|Other "false"表示不添加页面请求参数，其他都为添加
 * @param String over "true"|Other "true"表示用页面请求参数覆盖params，其他都为以params参数为准
 * @return 
 */
function appendParams(params,appendPage,over){
	var settings ={};
	var empty = {};
	if(!params){
		params = {}
	}
	var pageParams = getPageParams();
	if(!pageParams){
		pageParams = {};
	}
	var settings;
	if( "false" == appendPage){
		settings = params;
	}else{
	    if("true" == over){
		   settings =$.extend(empty,params,pageParams);
	    }else{
		   settings =$.extend(empty,pageParams,params);
	    }
	}
	var result = "";
	var appendQues = true;
	if(settings){
		for(param in settings){
			if(appendQues){
				result="?"+param+"="+settings[param];
				appendQues = false;
			}else{
				result =result+"&" +param+"="+settings[param];
			}
		}
	}
	return result;
}

function cmSelected(title,index,params){
	var obj;
	switch(index){
		//其他
		case -1:{
		  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/demo.jsp"};
		  break;
		}
		//首页信息
		case 0:{
		  obj = {title:title,closable:false,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/home.jsp"+appendParams(params)};
		  break;
		}
		//项目管理
		case 1:{
		  if("项目文档" == title){
			  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/browseDocuments.jsp"};
		  }else if("项目文档111" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/certification/searchCertification.jsp"};
		  }else{
		      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}//检验数据
		case 2:{
		  if("投标记录" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/tender/createTender.jsp"};
		  }else if("创建检验数据" == title){
		  		obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/examine/inspectionDocumentFound.jsp"};
		  }else{		
		  		obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}//代理报检
		case 3:{
		  if("费用报销" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/reimbursement/browseReimbursement.jsp"};
		  }else{
		      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}
		case 4:{
			  if("人员信息" == title){
			  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/hr/browseUsers.jsp"};
			  }else{
			      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
			  }
			  break;
			}//文档管理
		case 5:{
		  if("创建文档" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/createDocument.jsp"};
		  }else if("浏览文档"==title){
			  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/browseDocuments.jsp"};
		  }else if("查询文档" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/searchDocuments.jsp"};
		  }else{
		      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}//人员管理
		case 6:{
		  if("权限设置" == title){
		  	  //在设置完shmged目录验证机制后，用下面的连接
		  	  //obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/users/managePower.jsp"};
		  	  obj = {title:title,href:pageBaseUrl+"netmarkets/jsp/com/shlcm/users/managePower.jsp"};
		  }else if("查询用户" == title){
			  title = "人员管理-"+title;
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/users/searchUsers.jsp"};
		  }else{
		      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}//统计分析
		case 7:{
		  if("数据统计" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/sa/statistical.jsp"};
		  }else if("数据分析" == title){
		  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/sa/analysis.jsp"};
		  }else{
		      obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/authentication.jsp"};
		  }
		  break;
		}case 8:{
			  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/workFlow/myWorkItems.jsp"};
			  break;
		}case 9:{
			  if("公司新闻" == title){
			  	  obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/createNewsEntry.jsp"};
			  }
			  break;
			}
		
		default:{
			obj = {title:title,href:pageBaseUrl+"shmged/jsp/com/shlcm/portal/main/tabDemo.jsp"};
		}
	}
	addTab(obj);
}


function cmOnclickSelected(obj,index){
	var title = $(obj).text();
	cmSelected(title,index);
}

//创建一个新的tab 文档信息
function addTab(tabObj,tabId,oid,docName){
	var defaultTabId = "cmTabs";
	if(tabId){
		defaultTabId = tabId;
	}
	var tabDefault = {
		href:pageBaseUrl+"shmged/jsp/com/shlcm/documents/docInfo.jsp?oid="+oid,
		title:"文档:"+docName,
		selected: true,
		closable:true
	};
	var options ;
	if(tabObj){
	    var empty = {};
		options = $.extend(empty,tabDefault,tabObj);
	}else{
		options = tabDefault;
	}
	if($('#'+defaultTabId).tabs("exists",options.title)){
		$('#'+defaultTabId).tabs('select',options.title);
	}else{
	    $('#'+defaultTabId).tabs('add',options);
	}
//	alert(tab.outerHeight()+":"+tab.innerHeight());
//    alert("index:"+index);
	//var oldHeight = p.panel('panel').outerHeight();  
	//setHeight();
}

function myformatter(date){
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function myparser(s){
	if (!s) return new Date();
	var ss = (s.split('-'));
	var y = parseInt(ss[0],10);
	var m = parseInt(ss[1],10);
	var d = parseInt(ss[2],10);
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	} else {
		return new Date();
	}
}

//解决IE下input标签只读状态下按backspace键页面会后退的问题在input元素中增加onkeydown="ieBackSpaceBug()"属性
function ieBackSpaceBug(){
	if(window.event.keyCode == 8){
		window.event.keyCode = 0;
	}
}

//金额小写转人民币大写
function convertCurrency(currencyDigits) {
	// Constants:
	var MAXIMUM_NUMBER = 99999999999.99;
	// Predefine the radix characters and currency symbols for output:
	var CN_ZERO = "零";
	var CN_ONE = "壹";
	var CN_TWO = "贰";
	var CN_THREE = "叁";
	var CN_FOUR = "肆";
	var CN_FIVE = "伍";
	var CN_SIX = "陆";
	var CN_SEVEN = "柒";
	var CN_EIGHT = "捌";
	var CN_NINE = "玖";
	var CN_TEN = "拾";
	var CN_HUNDRED = "佰";
	var CN_THOUSAND = "仟";
	var CN_TEN_THOUSAND = "万";
	var CN_HUNDRED_MILLION = "亿";
	var CN_SYMBOL = "";
	var CN_DOLLAR = "元";
	var CN_TEN_CENT = "角";
	var CN_CENT = "分";
	var CN_INTEGER = "整";

	// Variables:
	var integral; // Represent integral part of digit number.
	var decimal; // Represent decimal part of digit number.
	var outputCharacters; // The output result.
	var parts;
	var digits, radices, bigRadices, decimals;
	var zeroCount;
	var i, p, d;
	var quotient, modulus;

	// Validate input string:
	currencyDigits = currencyDigits.toString();
	if (currencyDigits == "") {
	alert("Empty input!");
	return "";
	}
	if (currencyDigits.match(/[^,.\d]/) != null) {
	alert("Invalid characters in the input string!");
	return "";
	}
	if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
	alert("Illegal format of digit number!");
	return "";
	}

	// Normalize the format of input digits:
	currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
	currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
	// Assert the number is not greater than the maximum number.
	if (Number(currencyDigits) > MAXIMUM_NUMBER) {
	alert("Too large a number to convert!");
	return "";
	}

	// Process the coversion from currency digits to characters:
	// Separate integral and decimal parts before processing coversion:
	parts = currencyDigits.split(".");
	if (parts.length > 1) {
	integral = parts[0];
	decimal = parts[1];
	// Cut down redundant decimal digits that are after the second.
	decimal = decimal.substr(0, 2);
	}
	else {
	integral = parts[0];
	decimal = "";
	}
	// Prepare the characters corresponding to the digits:
	digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
	radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
	bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
	decimals = new Array(CN_TEN_CENT, CN_CENT);
	// Start processing:
	outputCharacters = "";
	// Process integral part if it is larger than 0:
	if (Number(integral) > 0) {
	zeroCount = 0;
	for (i = 0; i < integral.length; i++) {
	p = integral.length - i - 1;
	d = integral.substr(i, 1);
	quotient = p / 4;
	modulus = p % 4;
	if (d == "0") {
	zeroCount++;
	}
	else {
	if (zeroCount > 0)
	{
	outputCharacters += digits[0];
	}
	zeroCount = 0;
	outputCharacters += digits[Number(d)] + radices[modulus];
	}
	if (modulus == 0 && zeroCount < 4) {
	outputCharacters += bigRadices[quotient];
	}
	}
	outputCharacters += CN_DOLLAR;
	}
	// Process decimal part if there is:
	if (decimal != "") {
	for (i = 0; i < decimal.length; i++) {
	d = decimal.substr(i, 1);
	if (d != "0") {
	outputCharacters += digits[Number(d)] + decimals[i];
	}
	}
	}
	// Confirm and return the final output string:
	if (outputCharacters == "") {
	outputCharacters = CN_ZERO + CN_DOLLAR;
	}
	if (decimal == "") {
	outputCharacters += CN_INTEGER;
	}
	outputCharacters = CN_SYMBOL + outputCharacters;
	return outputCharacters;
	}

	function reLoadTab(name)
	{
		$("#cmTabs").tabs('select',name);  
        var tab = $('#cmTabs').tabs('getSelected');       
        $("#cmTabs").tabs('update',{  
        tab:tab,  
        options:{  
           title:name,  
           style:{padding:'1px'},  
       //href:URL, // 使用href会导致页面加载两次，所以使用content代替   
           content:'<iframe name="indextab" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>',  
       closable:false,  
       fit:true,  
       selected:true,  
       closable:true
        }  
         }); 
	}
	
	
	
	//或得table选中的并删除
	function deleteSelections(tableID,tabName){
		var rows = $(tableID).datagrid('getSelections');
		var oids="";
		for(var i=0; i<rows.length; i++){
			var row = rows[i];
			oids+=row.id+"__";
		}
		if(rows.length>0)
		{
		deleteObj(oids,tabName);
		}
	}


	//删除文档
	function deleteObj(oids,tabName)
	{
		var rows = $('#rbTable').datagrid('getSelections');
		if(rows[0].state!="编制中")
		{
			$.messager.alert('提示',"非编制中状态不能删除");
			return;
		}
		var url=cmBaseUrl+'shmged/jsp/com/shlcm/documents/deleteObj.jsp';
		$.messager.confirm('提示', '确定删除费用报销单?', function(flag){
			  if(flag){
			    var idata = {oids:oids};
		        jQuery.post(url,idata,function (data){
		   	       if(data.indexOf("SUCC")!=-1){
		   	    	$.messager.alert('提示', "删除成功");
		   	    	reLoadTab(tabName);
		   	       }else{
		   	    	$.messager.alert('提示', data);
		   	       }
		        });
			 }
		  });
	}
	
	//下载附件
	function downloadFiles(tableId)
	{
		var rows = $(tableId).datagrid('getSelections');
		var oids="";
		for(var i=0; i<rows.length; i++){
			var row = rows[i];
			oids+=row.id+"__";
		}
		
		if(oids.length==0)
		{
			$.messager.alert('提示', "请选择要下载的文档");
			return;
		}
		window.location.href=cmBaseUrl+"shmged/jsp/com/shlcm/documents/downLoadFiles.jsp?oids="+oids;
	}
	
